define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');

    var zindex = 2147483647;

    var crmGridIntroduce = {
        showMode: function(id){
            $(document.body).append('<div id="'+id+'-mode" class="oa-shade-div"></div>');
            $('#'+id+'-mode').css({
                top: '0px',
                left: '0px',
                height: 'calc(100% + 30px)',
                zIndex: zindex-60
            }).show();
        },
        closeMode: function(id){
            $('#'+id+'-mode').remove();
        },
        introduceV36ForcastPosition: function() {
            //return;
            var me = this;
            var id = 'introduceV36ForcastPosition';
            if($.cookie(id)) return;
            var filterBtn = $('#crm_grid_con');
            if(filterBtn.size() > 0)return;//crm列表页面不显示，要不是就和客户的新手导引重复了

            //校验是否有销售漏斗预测的权限
            $.getJSON('/json/crm_salesanalysis/haveSalesForecastPermission.action').done(function(json) {
                if (json.status == 0) {
                    if(json.data && json.data.havePermission){
                        me.showMode(id);
                        require.async('./tmpl/menu.tpl', function(html){
                            $("body").append(rk.templateText(html));
                            //图片加载完成之后再去加载tip
                            $("#forecast_guider_menu_img").on('load', function(){
                                require.async('./tmpl/v36_forcast_position.tpl', function(html) {
                                    $('body').append(rk.templateText(html, {id: id}));
                                    var target = $("#forecast_guider_menu");
                                    $('#'+id).position({
                                        my: "right center",
                                        at: "left-10 center-20",
                                        of: target
                                    }).css('z-index', zindex);


                                    $('#'+id).find('a.pg-btn-white').click(function(){
                                        $.cookie(id, 'true', { expires: 7, path: '/' });
                                        $('#'+id).off().remove();
                                        $("#forecast_guider_left_menu").remove();
                                        $("#forecast_guider_menu").remove();
                                        me.closeMode(id);
                                    });
                                });
                            });
                        });
                    }
                }
            }).fail(function(def, code, message) {
                
            });
        },
        introduceFilterSortStep1: function() {
            var me = this;
            var id = 'introduceFilterSortStep1';
            if($.cookie(id)) return;

            var filterBtn = $('#crm_grid_con');
            if(filterBtn.size() == 0)return;//非crm页面不显示
            if(seajs.data.isCRMFinal)return;//crm详情页面不显示

            me.showMode(id);

            var timer;
            var counter = 0;
            function waitForGrid(){
                if(counter > 10) window.clearTimeout(timer);
                timer = window.setTimeout(function(){
                    if($('#crm_grid_con').find('.iconscontainer').size()>0){
                        require.async('./tmpl/v36_filter_and_sort1.tpl', function(html) {
                            me._introduceFilterSortStep1(html, id);
                        });
                        window.clearTimeout(timer);
                    }else{
                        waitForGrid();
                    }
                    counter++;
                }, 500);
            }
            waitForGrid();
        },
        _introduceFilterSortStep1: function(html, id){
            var me = this;
            $('#pageleft').append(rk.templateText(html, {id: id}));

            var eq = $('.iconscontainer:visible').size() > 3 ? 2 : 1;
            var target = $('.iconscontainer:visible:eq('+eq+')');
            target.attr('for-demo', 'true');
            target = target.next();
            target.show()
            $('#'+id).position({
              my: "left center",
              at: "right+10 center+72",
              of: target
            }).css('z-index', zindex);

            $('#'+id+'-spot').position({
              my: "center center",
              at: "center-13 center",
              of: target
            }).css('z-index', zindex);

            $('#'+id).find('a.pg-btn-white').click(function(){
                //$.cookie(id, 'true', { expires: 7, path: '/' });   
                $('#'+id).off().remove(); 
                $('#'+id+'-spot').off().remove();   
                target.hide();
                require.async('./tmpl/v36_filter_and_sort2.tpl', function(tpl) {
                    me.introduceFilterSortStep2(tpl, id);       
                }); 
            });
        },
        introduceFilterSortStep2: function(tpl, id){
            var me = this;

            $('.iconscontainer[for-demo="true"]').hide();

            var html = $.parseTpl(tpl);
            $('#pageleft').append(rk.templateText(html.popup, {id: id}));
            $('#pageleft').append(rk.templateText(html.conditionimage, {id: id}));
            var target = $('.grid-view-filterresult');

            $('#'+id).position({
              my: "left top",
              at: "left bottom+20",
              of: target
            }).css('z-index', zindex);

            $('#'+id+'-conditions').position({
              my: "left center",
              at: "left center",
              of: target
            }).css('z-index', zindex);

            $('#'+id).find('a.pg-btn-white').click(function(){
                $.cookie(id, 'true', { expires: 7, path: '/' });
                $('#'+id).off().remove();   
                $('#'+id+'-conditions').remove();
                me.closeMode(id);
            });
        }
    };



    module.exports = crmGridIntroduce;
});
